<?php
//*********************************************************************//
// Function Name:ControllerShopsShop
// Description:
// Developer  :Hoa Tran
// Date		  :Nov 23, 2009
// Version    :V01
//*********************************************************************//
class ControllerShopsShop extends Controller {
	public function index() {
		//-------------------------------------------------------------------------//
		// Check login
		//-------------------------------------------------------------------------//
		$this->language->load('common/header');
		$this->language->load('common/home');
		$this->language->load('module/shops');
		$this->load->model('upload/shop');
		$this->load->module('shops/productrelated');
		$this->load->module('shops/customerinfo');
		$this->load->model('product_to_catalog/select');

		//-------------------------------------------------------------------------//
		// get Shop Name
		//-------------------------------------------------------------------------//
		$shop = "";
		$shop_id = "";
		$id_sh = "";
		if(isset($this->request->get['shop'])){
			$id_sh = $this->request->get['shop'];
		}
		$customer_id = $this->customer->getId();
		if(mb_strlen($id_sh) > 0 ){
			$shopInfo = $this->model_upload_shop->getShopInfoByShopId($id_sh);
		} else if(mb_strlen($customer_id) > 0){
			$shopInfo = $this->model_upload_shop->getShopNameByCustomerId($customer_id);
		}
		$shop_name = "";
		if(empty($shopInfo) == false)
		{
			$shop_id   = $shopInfo[0]['shop_id'];
			$shop_name = $shopInfo[0]['shop_name'];
		}
		$this->document->shop_name = $shop_name;

		if(empty($shopInfo) == true)
		{
			$this->redirect("?route=common/home");
		}

		$zone_id = "";
		if (isset($this->request->get['zone_id'])) {
			$zone_id = $this->request->get['zone_id'];
		} else {
			$zone_id = '';
		}

		//-------------------------------------------------------------------------//
		// get Relation product
		//-------------------------------------------------------------------------//
		$relationPrd = $this->module_shops_productrelated->productRelated($shop_id, 10, $zone_id);
		$this->document->relationPrd = $relationPrd;
		$this->document->shop_id     = $shop_id;
		//-------------------------------------------------------------------------//
		// get user infomation
		//-------------------------------------------------------------------------//
		$this->userInfomation = $this->config->get('config_template') . 'product/customer_info.phtml';
		$userInfomation = $this->fetch($this->userInfomation);
		$this->document->userInfomation = $userInfomation;


		$this->language->load('product/category');
		$this->document->breadcrumbs = array();
   		$this->document->breadcrumbs[] = array(
      		'href'      => $this->url->http('common/home'),
       		'text'      => $this->language->get('text_home'),
       		'separator' => FALSE
   		);
		$this->load->model('tool/seo_url');
		$this->data['text_sort'] = $this->language->get('text_sort');
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.date_added';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}

		$url = '';
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		$this->load->model('catalog/product');
		$this->load->helper('image');

		$this->data['zone_id'] = $zone_id;

		$product_total = $this->model_catalog_product->getTotalProductsByShopId($shop_id, $zone_id);
		if ( $product_total > 0 ) {
			$this->load->model('catalog/review');
			$this->data['products'] = array();
			$results = $this->model_catalog_product->getProductsByShopId($shop_id, $zone_id, $sort, $order, ($page - 1) * 12, 12);
        	foreach ($results as $result) {
				if ($result['image']) {
					$image = $result['image'];
				} else {
					$image = 'no_image.jpg';
				}
				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
				if ($special) {
					$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = FALSE;
				}

				$rating = $this->model_catalog_review->getAverageRating($result['product_id']);
				$path    = $this->model_product_to_catalog_select->getListCategories($result['product_id']);

				$date_add = date_create($result['date_added']);
				$this->data['products'][] = array(
					'product_id' =>$result['product_id'],
            		'name'    => $result['name'],
					'shop_name'   => $result['shop_name'],
					'href_shop'   => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop&shop=' . $result['shop_id'])),
					'zone'   => $result['zone'],
        			'rating'  => $rating,
				 	'viewed'  => $result['viewed'],
					'date_added'  => date_format ($date_add,"d:m:Y"),
            		'rating'  => $rating,
					'stars'   => sprintf($this->language->get('text_stars'), $rating),
					'thumb'   => image_resize($image,120,120),
            		'price'   => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
					'special' => $special,
					'href'    => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop_customer&path='.$path.'&product_id=' . $result['product_id']))
          		);
        	}

			if (!$this->config->get('config_customer_price')) {
				$this->data['display_price'] = TRUE;
			} elseif ($this->customer->isLogged()) {
				$this->data['display_price'] = TRUE;
			} else {
				$this->data['display_price'] = FALSE;
			}
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			$pagination = new Pagination1();
			$pagination->total = $product_total;
			$pagination->page = $page;
			$pagination->limit = 12;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . $url . '&page=%s'));
			$this->data['pagination'] = $pagination->render();
			$this->data['sort']  = $sort;
			$this->data['order'] = $order;
			$this->data['shop_name'] = $shop;
			$this->data['welcom'] = $this->language->get('welcom');
      	}

      	$this->data['sorts'] = array();

		$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_update_desc'),
				'value' => 'p.date_added-DESC',
				'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.date_added&order=DESC'))
		);

		$this->data['sorts'][] = array(
			'text'  => $this->language->get('text_price_asc'),
			'value' => 'p.price-ASC',
			'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.price&order=ASC'))
		);
		$this->data['sorts'][] = array(
			'text'  => $this->language->get('text_price_desc'),
			'value' => 'p.price-DESC',
			'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.price&order=DESC'))
		);
		//-------------------------------------------------------------------------//
		//Begin Add code by D-Ngan Aug 20 2012
			$this->data['href_modify'] = $this->url->http('upload/edit_product&amp;product_id=');
			$this->data['href_delete'] = $this->url->http('upload/product/delete&product_id=');		
		//End add code by D0Ngan Aug 20 2012
		//-------------------------------------------------------------------------//
		// load library
		//-------------------------------------------------------------------------//
		$this->load->helper('image');
		$this->load->model('localisation/zone');
		//----------------------------------------------------------------------------------//
		// get area
		//----------------------------------------------------------------------------------//
    	$area = $this->model_localisation_zone->getZonesByCountryId(230);
    	$this->data['areas']  = $area;

      	$this->id       = 'content';
		$this->template = $this->config->get('config_template') . 'shop/shop.phtml';
		$this->layout   = 'common/shop_layout_list';
		
		$this->render();
  	}

	public function ajax() {
		
		//-------------------------------------------------------------------------//
		// Check login
		//-------------------------------------------------------------------------//
		$this->language->load('common/header');
		$this->language->load('common/home');
		$this->language->load('module/shops');
		$this->load->model('upload/shop');
		$this->load->module('shops/productrelated');
		$this->load->module('shops/customerinfo');
		$this->load->model('product_to_catalog/select');
		
		//-------------------------------------------------------------------------//
		// get Shop Name
		//-------------------------------------------------------------------------//
		$shop = "";
		$shop_id = "";
		if(isset($this->request->get['shop'])){
			$shop = $this->request->get['shop'];
		}
		
		$customer_id = $this->customer->getId();
		if(mb_strlen($shop) > 0 ){
			$shopInfo = $this->model_upload_shop->getShopInfoByShopName($shop);
		} else if(mb_strlen($customer_id) > 0){
			$shopInfo = $this->model_upload_shop->getShopNameByCustomerId($customer_id);
			
		}
		
		$shop_name = "";
		if(empty($shopInfo) == false)
		{
			$shop_id   = $shopInfo[0]['shop_id'];
			$shop_name = $shopInfo[0]['shop_name'];
		}
		$this->document->shop_name = $shop_name;

		if(empty($shopInfo) == true)
		{
			$this->redirect("common/home");
		}

		$zone_id = "";
		if (isset($this->request->get['zone_id'])) {
			$zone_id = $this->request->get['zone_id'];
		} else {
			$zone_id = '';
		}
		//-------------------------------------------------------------------------//
		// get Relation product
		//-------------------------------------------------------------------------//
		$relationPrd = $this->module_shops_productrelated->productRelated($shop_id, 10, $zone_id);
		$this->document->relationPrd = $relationPrd;
		$this->document->shop_id     = $shop_id;
		//-------------------------------------------------------------------------//
		// get user infomation
		//-------------------------------------------------------------------------//
		
		$this->userInfomation = $this->config->get('config_template') . 'product/customer_info.phtml';
		$userInfomation = $this->fetch($this->userInfomation);
		$this->document->userInfomation = $userInfomation;
		
		$this->language->load('product/category');
		$this->document->breadcrumbs = array();
   		$this->document->breadcrumbs[] = array(
      		'href'      => $this->url->http('common/home'),
       		'text'      => $this->language->get('text_home'),
       		'separator' => FALSE
   		);
		$this->load->model('tool/seo_url');
		$this->data['text_sort'] = $this->language->get('text_sort');
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.date_added';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}

		$url = '';
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		$this->load->model('catalog/product');
		$this->load->helper('image');

		$this->data['zone_id'] = $zone_id;
		
		$product_total = $this->model_catalog_product->getTotalProductsByShopId($shop_id, $zone_id);
		if ( $product_total > 0 ) {
			$this->load->model('catalog/review');
			$this->data['products'] = array();
			$results = $this->model_catalog_product->getProductsByShopId($shop_id, $zone_id, $sort, $order, ($page - 1) * 12, 12);
        	foreach ($results as $result) {
				if ($result['image']) {
					$image = $result['image'];
				} else {
					$image = 'no_image.jpg';
				}
				$special = $this->model_catalog_product->getProductSpecial($result['product_id']);
				if ($special) {
					$special = $this->currency->format($this->tax->calculate($special, $result['tax_class_id'], $this->config->get('config_tax')));
				} else {
					$special = FALSE;
				}

				$rating = $this->model_catalog_review->getAverageRating($result['product_id']);
				$path    = $this->model_product_to_catalog_select->getListCategories($result['product_id']);

				$date_add = date_create($result['date_added']);
				$this->data['products'][] = array(
					'product_id' =>$result['product_id'],
            		'name'    => $result['name'],
					'shop_name'   => $result['shop_name'],
					'href_shop'   => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop&shop=' . $result['shop_name'])),
					'zone'   => $result['zone'],
        			'rating'  => $rating,
				 	'viewed'  => $result['viewed'],
					'date_added'  => date_format ($date_add,"d:m:Y"),
            		'rating'  => $rating,
					'stars'   => sprintf($this->language->get('text_stars'), $rating),
					'thumb'   => image_resize($image,120,120),
            		'price'   => $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax'))),
					'special' => $special,
					'href'    => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop_customer&path='.$path.'&product_id=' . $result['product_id']))
          		);
        	}
			
			if (!$this->config->get('config_customer_price')) {
				$this->data['display_price'] = TRUE;
			} elseif ($this->customer->isLogged()) {
				$this->data['display_price'] = TRUE;
			} else {
				$this->data['display_price'] = FALSE;
			}
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			$pagination = new Pagination1();
			$pagination->total = $product_total;
			$pagination->page = $page;
			$pagination->limit = 12;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . $url . '&page=%s'));
			$this->data['pagination'] = $pagination->render();
			$this->data['sort']  = $sort;
			$this->data['order'] = $order;
			$this->data['shop_name'] = $shop;
			$this->data['welcom'] = $this->language->get('welcom');
      	}

      	$this->data['sorts'] = array();

		$this->data['sorts'][] = array(
				'text'  => $this->language->get('text_update_desc'),
				'value' => 'p.date_added-DESC',
				'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.date_added&order=DESC'))
		);

		$this->data['sorts'][] = array(
			'text'  => $this->language->get('text_price_asc'),
			'value' => 'p.price-ASC',
			'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.price&order=ASC'))
		);
		$this->data['sorts'][] = array(
			'text'  => $this->language->get('text_price_desc'),
			'value' => 'p.price-DESC',
			'href'  => $this->model_tool_seo_url->rewrite($this->url->http('shops/shop/ajax&shop=' . $shop . '&sort=p.price&order=DESC'))
		);
		//Begin Add code by D-Ngan Aug 20 2012
			$this->data['href_modify'] = $this->url->http('upload/edit_product&product_id=');
			$this->data['href_delete'] = $this->url->http('upload/product/delete&product_id=');		
		//End add code by D0Ngan Aug 20 2012
		//-------------------------------------------------------------------------//
		// load library
		//-------------------------------------------------------------------------//
		$this->load->helper('image');
		$this->load->model('localisation/zone');
		//----------------------------------------------------------------------------------//
		// get area
		//----------------------------------------------------------------------------------//
    	$area = $this->model_localisation_zone->getZonesByCountryId(230);
    	$this->data['areas']  = $area;
		$this->template = $this->config->get('config_template') . 'shop/shop_ajax.phtml';
		$json['html'] = $this->fetch($this->template);
		$json['success'] = true;
		$this->load->library('json');
		$this->response->setOutput(Json::encode($json));
		
		//$this->id       = 'content';
		//$this->template = $this->config->get('config_template') . 'shop/shop_ajax.phtml';
		//$this->layout   = 'common/shop_layout_list';
		//$this->render();
		
  	}// aject function 
}
?>